package com.baobao.dao;

import com.baobao.domain.Chat;

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Chat entities.
 * 
 */
@Repository("ChatDAO")
@Transactional
public class ChatDAOImpl extends AbstractJpaDao<Chat> implements ChatDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Chat.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new ChatDAOImpl
	 *
	 */
	public ChatDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findChatByTextContaining
	 *
	 */
	@Transactional
	public Set<Chat> findChatByTextContaining(String text) throws DataAccessException {

		return findChatByTextContaining(text, -1, -1);
	}

	/**
	 * JPQL Query - findChatByTextContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByTextContaining(String text, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByTextContaining", startResult, maxRows, text);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByReceiverContaining
	 *
	 */
	@Transactional
	public Set<Chat> findChatByReceiverContaining(String receiver) throws DataAccessException {

		return findChatByReceiverContaining(receiver, -1, -1);
	}

	/**
	 * JPQL Query - findChatByReceiverContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByReceiverContaining(String receiver, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByReceiverContaining", startResult, maxRows, receiver);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByReceiver
	 *
	 */
	@Transactional
	public Set<Chat> findChatByReceiver(String receiver) throws DataAccessException {

		return findChatByReceiver(receiver, -1, -1);
	}
	


	/**
	 * JPQL Query - findChatByReceiver
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByReceiver(String receiver, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByReceiver", startResult, maxRows, receiver);
		return new LinkedHashSet<Chat>(query.getResultList());
	}
	
	

	

	/**
	 * JPQL Query - findChatByStatusContaining
	 *
	 */
	@Transactional
	public Set<Chat> findChatByStatusContaining(String status) throws DataAccessException {

		return findChatByStatusContaining(status, -1, -1);
	}

	/**
	 * JPQL Query - findChatByStatusContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByStatusContaining(String status, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByStatusContaining", startResult, maxRows, status);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByText
	 *
	 */
	@Transactional
	public Set<Chat> findChatByText(String text) throws DataAccessException {

		return findChatByText(text, -1, -1);
	}

	/**
	 * JPQL Query - findChatByText
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByText(String text, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByText", startResult, maxRows, text);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByDate
	 *
	 */
	@Transactional
	public Set<Chat> findChatByDate(java.util.Calendar date) throws DataAccessException {

		return findChatByDate(date, -1, -1);
	}

	/**
	 * JPQL Query - findChatByDate
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByDate(java.util.Calendar date, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByDate", startResult, maxRows, date);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByStatus
	 *
	 */
	@Transactional
	public Set<Chat> findChatByStatus(String status) throws DataAccessException {

		return findChatByStatus(status, -1, -1);
	}

	/**
	 * JPQL Query - findChatByStatus
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByStatus(String status, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByStatus", startResult, maxRows, status);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByPrimaryKey
	 *
	 */
	@Transactional
	public Chat findChatByPrimaryKey(String sender, String receiver, java.util.Calendar date) throws DataAccessException {

		return findChatByPrimaryKey(sender, receiver, date, -1, -1);
	}
	
	

	/**
	 * JPQL Query - findChatByPrimaryKey
	 *
	 */

	@Transactional
	public Chat findChatByPrimaryKey(String sender, String receiver, java.util.Calendar date, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findChatByPrimaryKey", startResult, maxRows, sender, receiver, date);
			return (com.baobao.domain.Chat) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}
	

	
	/**
	 * JPQL Query - findChatByStatus
	 *
	 */
	@Transactional
	public Set<Chat> findChatByPhoneAndStatus(String sender, String receiver, String status) throws DataAccessException {

		return findChatByPhoneAndStatus(sender, receiver, status, -1, -1);
	}

	/**
	 * JPQL Query - findChatByPhoneAndStatus
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByPhoneAndStatus(String sender, String receiver, String status, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByPhoneAndStatus", startResult, maxRows, sender,receiver,status);
		return new LinkedHashSet<Chat>(query.getResultList());
	}
	
	
	
	@Transactional
	public Set<Chat> findChatByReceiverAndLatestMsgnum(String receiver, Calendar startDate,Integer msgnum) throws DataAccessException {

		return findChatByReceiverAndLatestMsgnum(receiver,startDate, -1, msgnum);
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByReceiverAndLatestMsgnum(String receiver,Calendar startDate,int startResult, int maxRows) throws DataAccessException{
		Query query = createNamedQuery("findChatByReceiverAndLatestMsgnum", startResult, maxRows, receiver,startDate);
		return new LinkedHashSet<Chat>(query.getResultList());
	}
	

	@Transactional
	public Set<Chat> findChatByMaxDateAndPhone(String sender, String receiver) throws DataAccessException {

		return findChatByMaxDateAndPhone(sender, receiver, -1, -1);
	}


	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByMaxDateAndPhone(String sender, String receiver,int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByMaxDateAndPhone", startResult, maxRows, sender,receiver);
		return new LinkedHashSet<Chat>(query.getResultList());
	}
	

	/**
	 * JPQL Query - findChatBySenderContaining
	 *
	 */
	@Transactional
	public Set<Chat> findChatBySenderContaining(String sender) throws DataAccessException {

		return findChatBySenderContaining(sender, -1, -1);
	}

	/**
	 * JPQL Query - findChatBySenderContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatBySenderContaining(String sender, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatBySenderContaining", startResult, maxRows, sender);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatByUniqueId
	 *
	 */
	@Transactional
	public Set<Chat> findChatByUniqueId(Integer uniqueId) throws DataAccessException {

		return findChatByUniqueId(uniqueId, -1, -1);
	}

	/**
	 * JPQL Query - findChatByUniqueId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatByUniqueId(Integer uniqueId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatByUniqueId", startResult, maxRows, uniqueId);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllChats
	 *
	 */
	@Transactional
	public Set<Chat> findAllChats() throws DataAccessException {

		return findAllChats(-1, -1);
	}

	/**
	 * JPQL Query - findAllChats
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findAllChats(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllChats", startResult, maxRows);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * JPQL Query - findChatBySender
	 *
	 */
	@Transactional
	public Set<Chat> findChatBySender(String sender) throws DataAccessException {

		return findChatBySender(sender, -1, -1);
	}

	/**
	 * JPQL Query - findChatBySender
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Chat> findChatBySender(String sender, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findChatBySender", startResult, maxRows, sender);
		return new LinkedHashSet<Chat>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Chat entity) {
		return true;
	}
}
